package com.okilo.online.app.tools.helper

import com.okilo.online.app.BuildConfig
import com.okilo.online.app.OkiloApplication
import com.okilo.online.app.R
import com.okilo.online.app.tools.shared.OkiloSharedHp
import com.okilo.online.app.tools.shared.OkiloSharedKey
import org.json.JSONObject

/**
 * ConfigsHelper
 * @Author wwdeng
 * @CreateTime: 2024/11/28 12:04
 * @Annotation：自定义数据 帮助类
 */
object OkiloConfigCustomerHp {
    //缓存弹出公告信息的次数，此值只会在：审核中、放款中、待还款改变
    var showOkiloNoticeCount = 0

    /**
     * 自定义数据来源
     *     1：客服配置
     *
     * "customData": {
     *         "workDate": "Monday to Sunday",
     *         "workTime": "9:00am to 6:30pm",
     *         "isShowOcrNoDocuments":"YES" 是否支持不上传证件照
     *         "deleteDataDesc": "删除数据描述",
     *         "isShowCustomerChat": "YES", 是否显示在线客服功能
     *         "release":{
     *              "isMustUpdate":true,
     *              "versionCode":"1",
     *              "versionName":"1.0.0",
     *              "versionRemark":""//更新描述 需用"$"分割
     *         },
     *         "noticeInfo": {
     *             "num":3,
     *             "isShow": "YES",
     *             "noticeTitle": "Avis",
     *             "noticeText": "Veuillez ne pas transférer les remboursements sur un compte privé pour éviter toute fraude ou perte !"
     *         },
     *     },
     */

    /**
     * 保存客服配置数据
     */
    fun sOkiloConfigCustomerData(data: String){
        OkiloSharedHp.sOkiloSharedData(OkiloSharedKey.SP_OKILO_COMMON,data)
    }

    /**
     * 获取客服配置
     */
    fun gOkiloConfigCustomerData(): JSONObject? {
        val dataString = OkiloSharedHp.gOkiloSharedString(OkiloSharedKey.SP_OKILO_COMMON)
        if (dataString.isEmpty()){
            return null
        }
        val data = JSONObject(dataString)
        if (data.has("customize")){
            return data.optJSONObject("customize")
        }
        return null
    }

    /**
     * 审核账号
     */
    fun gOkiloConfigCustomerAdminList(): ArrayList<JSONObject>?{
        val customize = gOkiloConfigCustomerData()
        if (customize != null) {
            val accList = arrayListOf<JSONObject>()
            customize.optJSONArray("accList")?.let { list ->
                for (i in 0 until list.length()) {
                    accList.add(list.optJSONObject(i))
                }
            }
            return accList
        }
        return null
    }

    /**
     * 审核账号
     */
    fun gOkiloConfigCustomerAdminMaps(): Map<String, JSONObject>?{
        val customize = gOkiloConfigCustomerData()
        if (customize != null) {
            val accList = hashMapOf<String, JSONObject>()
            customize.optJSONArray("accList")?.let { list ->
                for (i in 0 until list.length()) {
                    val item = list.optJSONObject(i)
                    accList.put(item.optString("accM"),item)
                }
            }
            return accList
        }
        return null
    }

    /**
     * 获取客服配置里自定义数据
     * customData
     */
    fun gOkiloConfigCustomerCustomData(): JSONObject?{
        val customize = gOkiloConfigCustomerData()
        if (customize != null) {
            return customize.optJSONObject("customData")
        }
        return null
    }

    /**
     * 进件信息 - 证件照 是否支持选择不传
     */
    fun iOkiloOcrNoDocuments(): Boolean {
        val customData = gOkiloConfigCustomerCustomData() ?: return false
        if (customData.has("isShowOcrNoDocuments")){
            return customData.optString("isShowOcrNoDocuments") == "YES"
        }
        return false
    }

    /**
     * 删除个人数据描述
     */
    fun gOkiloDeleteUserDataDesc(): String {
        val customData = gOkiloConfigCustomerCustomData() ?: return OkiloApplication.app.getString(R.string.okilo_data_clear_tips)
        if (customData.has("deleteDataDesc")){
            return customData.optString("deleteDataDesc")
        }
        return OkiloApplication.app.getString(R.string.okilo_data_clear_tips)
    }

    /**
     * 是否显示在线客服入口
     */
    fun iOkiloCustomerChat(): Boolean{
        val customData = gOkiloConfigCustomerCustomData() ?: return false
        if (customData.has("isShowCustomerChat")){
            return customData.optString("isShowCustomerChat") == "YES"
        }
        return false
    }

    /**
     * 获取 审核中、放款中 显示的公告信息
     */
    fun tOkiloApplyNoticeInfo(): JSONObject?{
        val customData = gOkiloConfigCustomerCustomData() ?: return null

        var noticeData: JSONObject? = null
        if (customData.has("noticeInfo")){
            noticeData = customData.optJSONObject("noticeInfo")
        }
        return noticeData
    }

    /**
     * 审核中、放款中、待还款是否显示公告信息
     */
    fun iOkiloApplyNoticeShow(): Boolean{
        val noticeInfo = tOkiloApplyNoticeInfo() ?: return false
        if (noticeInfo.has("isShow")){
            return noticeInfo.optString("isShow","NO") == "YES"
        }
        return false
    }

    /**
     * 获取版本更新信息
     */
    fun gOkiloReleaseObject(): JSONObject?{
        val customData = gOkiloConfigCustomerCustomData() ?: return null
        if (!customData.has("release")){
            return null
        }
        return customData.optJSONObject("release")
    }

    /**
     * 获取是否有强制更新（启动页使用）
     *  "isMustUpdate":true,
     *  "versionCode":"1",
     *  "versionName":"1.0.0",
     *  "versionRemark":"1xxx$2xxx$3xxx"
     */
    fun iOkiloReleaseMustUpdate(): Boolean {
        val versionObj = gOkiloReleaseObject() ?: return false
        val versionCode = versionObj.optInt("versionCode",1)
        val isMustUpdate = versionObj.optBoolean("isMustUpdate",false)
        return BuildConfig.VERSION_CODE < versionCode && isMustUpdate
    }

    /**
     * 获取是否有更新（首页使用）
     */
    fun iOkiloReleaseUpdate(): Boolean{
        val versionObj = gOkiloReleaseObject() ?: return false
        val versionCode = versionObj.optInt("versionCode",1)
        return BuildConfig.VERSION_CODE < versionCode
    }
}